package CRUD;

// default package
// Generated Apr 21, 2011 3:13:26 PM by Hibernate Tools 3.3.0.GA

import Utility.SessionFactoryUtil;
import java.util.List;
import javax.naming.InitialContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Example;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import xmlMapping.Member;
/**
 * Home object for domain model class Member.
 * @see .Member
 * @author Hibernate Tools
 */
public class MemberHome {

	private static final Log log = LogFactory.getLog(MemberHome.class);

      	public Session session;

    public MemberHome() {
           if(session==null|| !session.isOpen())
           
        session=SessionFactoryUtil.getSessionFactory().openSession();
       
    }

	public boolean persist(Member transientInstance) {
         
                session.beginTransaction();
		log.debug("persisting Member instance");
		try {
			session.persist(transientInstance);
                        session.getTransaction().commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			//log.error("persist failed", re);
                        return false;
			//throw re;
		}
                return true;
	}

       

	public void attachDirty(Member instance) {
         
                session.beginTransaction();
		log.debug("attaching dirty Member instance");
		try {
			session.saveOrUpdate(instance);
                        session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
                        session.getTransaction().rollback();
                        System.out.println("attach dirty exception :((");
			throw re;
		}
	}

	public void attachClean(Member instance) {
         
                session.beginTransaction();
		log.debug("attaching clean Member instance");
		try {
			session.lock(instance, LockMode.NONE);
                        session.getTransaction().commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(Member persistentInstance) {
         
                session.beginTransaction();
		log.debug("deleting Member instance");
		try {
			session.delete(persistentInstance);
                         session.getTransaction().commit();
                        log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Member merge(Member detachedInstance) {
         
                session.beginTransaction();
		log.debug("merging Member instance");
		try {
			Member result = (Member) session.merge(detachedInstance);
                        session.getTransaction().commit();
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Member findById(java.lang.Integer id) {
         
                session.beginTransaction();
            System.out.println("find by id "+id);
		log.debug("getting Member instance with id: " + id);
		try {
			Member instance = (Member) session.get(
					Member.class, id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

        /*****************Hussein**************************/
        public List findMemberByType(String type){
         
                session.beginTransaction();
            Criteria crit=  session.createCriteria(Member.class);
            Criterion catId=Restrictions.eq("type", type);
            crit.add(catId);
            return crit.list();
        }
        /*****************************************************************/

	public List findByExample(Member instance) {
         
                session.beginTransaction();
		log.debug("finding Member instance by example");
		try {
			List results = session.createCriteria(
					Member.class).add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

        public Member findByUserName(String userName){
         
                session.beginTransaction();
            log.debug("getting Member instance with usernmae: " + userName);
		try {
                        Criteria crit = session.createCriteria(Member.class);
                        Criterion nameEq = Restrictions.eq("userName",userName);
                        Query q = session.createQuery("from Member m where m.userName like :userName ").setString("userName", userName);
                        crit.add(nameEq);
			Member instance = (Member)crit.uniqueResult();
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
        }

         public Member login(Member member){
          
                session.beginTransaction();
            boolean result;
            Criteria crit=  session.createCriteria(Member.class);
            Criterion username=Restrictions.eq("userName", member.getUserName());
            Criterion pass=Restrictions.eq("password", member.getPassword());
            Criterion cond=Restrictions.and(username, pass);
            crit.add(cond);
            return (Member)crit.uniqueResult();
        }
         public List search(String key){
                session.beginTransaction();
            Criteria crit=  session.createCriteria(Member.class);
            Criterion name=Restrictions.like("userName", key,MatchMode.ANYWHERE);
            crit.add(name);
            return crit.list();
        }
       }
